﻿using Digitalmes.Application.Scada.Events;
using Digitalmes.Application.Scada.Services;

namespace Digitalmes.Application.Scada.Commands;

/// <summary>
/// 进站请求命令处理者。
/// </summary>
internal sealed class InboundCommandHandler(IMediator sender, IServiceScopeFactory serviceScopeFactory) : IRequestHandler<InboundCommand, ScadaResult>
{
    public async Task<ScadaResult> Handle(InboundCommand request, CancellationToken cancellationToken)
    {
        using var scope = serviceScopeFactory.CreateScope();
        var inboundService = scope.ServiceProvider.GetRequiredService<IInboundApplicationService>();
        var result = await inboundService.EntryAsync(new InboundInput()).ConfigureAwait(false);

        // 发布产品存档后处理事件（不等待）
        await sender.Publish(new AfterInboundEvent(), cancellationToken).ConfigureAwait(false);

        return result;
    }
}
